#!/bin/sh

. /etc/rc.common

# Adempiere Service script for OS/X
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
export POSTGRES_HOME=/Library/PostgreSQL/8.3
export ADEMPIERE_HOME=/Applications/Adempiere
export PATH=$PATH:$JAVA_HOME/bin/:$POSTGRES_HOME/bin/
EXECDIR=/Applications/Adempiere
ENVFILE=/etc/profile
RETVAL=0
ADEMPIERESTATUS=
MAXITERATIONS=60 # 2 seconds every iteration, max wait 2 minutes)

RunService ()
{
    case $1 in
      start  ) StartService   ;;
      stop   ) StopService    ;;
      restart) RestartService ;;
      status ) StatusService ;;
      *      ) echo "$0: unknown argument: $1";;
    esac
}


StatusService ()
{
    ADEMPIERESTATUSSTRING=$(ps ax | grep -v grep | grep server| grep $EXECDIR)
    echo $ADEMPIERESTATUSSTRING | grep $EXECDIR 
    #ADEMPIERESTATUS=$?
}

StartService ()
{
    ADEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
   if [ $ADEMPIERESTATUS != 0 ] ; then
        ConsoleMessage "adempiere is already running"
        return 1
    fi
    ConsoleMessage "Starting ADempiere ERP: "
    export LOGFILE=$ADEMPIERE_HOME/jboss/server/adempiere/log/adempiere_`date +%Y%m%d%H%M%S`.log
    export LOOKFILE=$ADEMPIERE_HOME/jboss/server/adempiere/log/server.log
    su root -c "cd $EXECDIR/utils;$EXECDIR/utils/RUN_Server2.sh &> $LOGFILE &"
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        # wait for server to be confirmed as started in logfile
        STATUSTEST=0
        ITERATIONS=0
        while [ $STATUSTEST -eq 0 ] ; do
            sleep 2
            tail -n 5 $LOOKFILE | grep 'Server\].*Started in' &> /dev/null && STATUSTEST=1
            ITERATIONS=`expr $ITERATIONS + 1`
            if [ $ITERATIONS -gt $MAXITERATIONS ]
            then
                break
            fi
        done

if [ $STATUSTEST -eq 0 ]
        then
            ConsoleMessage "Service hasn't started within the timeout allowed, please review file $LOGFILE to see the status of the service"
        else
            ConsoleMessage "Adempiere Started"
        fi
        echo
    else
        ConsoleMessage "Admpiere Failed"
        echo
    fi
    return $RETVAL
}

StopService()
{
    ADEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
    if [ $ADEMPIERESTATUS = 0 ] ; then
        echo "adempiere is already stopped"
        return 1
    fi
    ConsoleMessage "Stopping ADempiere ERP: "
    export LASTLOG=`ls -t $ADEMPIERE_HOME/jboss/server/adempiere/log/adempiere_??????????????.log | head -1`
    export LOOKFILE=$ADEMPIERE_HOME/jboss/server/adempiere/log/server.log
    su root -c "cd $EXECDIR/utils;$EXECDIR/utils/RUN_Server2Stop.sh &> /dev/null &"
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        # wait for server to be confirmed as halted in logfile
        STATUSTEST=0
        ITERATIONS=0
        while [ $STATUSTEST -eq 0 ] ; do
            sleep 2
            tail -n 5 $LOOKFILE | grep 'Shutdown complete' &> /dev/null && STATUSTEST=1
            ITERATIONS=`expr $ITERATIONS + 1`
            if [ $ITERATIONS -gt $MAXITERATIONS ]
            then
                break
            fi
        done
        if [ $STATUSTEST -eq 0 ]
        then
            ConsoleMessage "Service hasn't stopped within the timeout allowed, please review file $LASTLOG to see the status of the service"
        else
            ConsoleMessage "Adempiere Stopped"
  fi
        echo
    else
        ConsoleMessage "Adempiere Failed to Stop"
        echo
    fi
    return $RETVAL

}


RestartService ()
{
    StopService
    sleep 2
    StartService
}


RunService "$1"
